package com.zen.android.sample.scananim.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.TextView;

import java.text.DecimalFormat;

/**
 * Created by Administrator on 2017/11/9 0009.
 */

public class RiseNumberTextView extends TextView implements IRaiseNumberListener {
    private static final int STOPPED = 0;
    private static final int RUNNING = 1;
    private int mPlayingState = STOPPED;
    private float toNumber;
    private float fromNumber;
    private long duration = 1111500;//500
    private int numberType = 2;
    private DecimalFormat fnum = new DecimalFormat("##0.00");
    private INumberChangedListener mEndListener = null;

    private ValueAnimator mFloatValueAnimator;
    private ValueAnimator mIntValueAnimator;
    private float mCurFloatNum;
    private float mCurIntNum;

    public RiseNumberTextView(Context context) {
        this(context, null);
    }

    public RiseNumberTextView(Context context, AttributeSet attr) {
        super(context, attr);
        setTextColor(Color.WHITE);
        setTextSize(30);
    }

    public RiseNumberTextView(Context context, AttributeSet attr, int defStyle) {
        super(context, attr, defStyle);
    }

    public boolean isRunning() {
        //返回当前运行状态
        return (mPlayingState == RUNNING);

    }

    public void setTextStr(float value) {
        setText(fnum.format(value));
    }

    private void runFloat() {
        runFloat(duration);
    }

    private static final long GB_SIZE = 1024;
    private void runFloat(long pDurtaion) {
        mFloatValueAnimator = ValueAnimator.ofFloat(fromNumber, toNumber);
        mFloatValueAnimator.setDuration(pDurtaion);
        mFloatValueAnimator
                .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        mCurFloatNum = Float.parseFloat(valueAnimator.getAnimatedValue().toString());
                        float tempsize = mCurFloatNum;
                        //如果值大于GB,需要转换
                        if(mCurFloatNum >= GB_SIZE){
                            tempsize = mCurFloatNum / GB_SIZE;
                            if(mEndListener != null){
                                mEndListener.onNumOverGb();
                            }
                        }
                        //针对gb转换
                        //
                        setText(fnum.format(tempsize));
                        if (valueAnimator.getAnimatedFraction() >= 1) {
                            //大于等于1时认为动画运行结束
                            mPlayingState = STOPPED;
                            if (mEndListener != null)
                                mEndListener.onEndFinish();
                        }
                    }

                });
        mFloatValueAnimator.start();
    }

    private void runInt() {
        mIntValueAnimator = ValueAnimator.ofInt((int) fromNumber,
                (int) toNumber);
        mIntValueAnimator.setDuration(duration);
        mIntValueAnimator
                .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        mCurIntNum = Integer.parseInt(valueAnimator.getAnimatedValue().toString());
                        setText(String.valueOf(mCurIntNum));
                        if (valueAnimator.getAnimatedFraction() >= 1) {
                            //置标志位
                            mPlayingState = STOPPED;
                            if (mEndListener != null)
                                mEndListener.onEndFinish();
                        }
                    }
                });
        mIntValueAnimator.start();
    }

    @Override
    public void start() {
        if (!isRunning()) {
            mPlayingState = RUNNING;
            if (numberType == 1)
                runInt();
            else
                runFloat();
        }
    }

    @Override
    public void setFloat(float fromNum, float toNum) {
        mCurFloatNum = fromNum;
        toNumber = toNum;
        numberType = 2;
        fromNumber = fromNum;
    }

    @Override
    public void setInteger(int fromNum, int toNum) {
        toNumber = toNum;
        numberType = 1;
        fromNumber = fromNum;
    }

    //数字增加时需要修改动画结果

    public void setFloatDur(float toNum,long pDuration){
        if(mFloatValueAnimator != null){
            mFloatValueAnimator.cancel();
        }
        fromNumber = mCurFloatNum;
        toNumber = toNum;
        numberType = 2;
        mPlayingState = RUNNING;
        long dur = pDuration > 0 ? pDuration : duration;
        runFloat(dur);

    }
    public void setInt(int toNum){
        if(mIntValueAnimator != null){
            mIntValueAnimator.cancel();
        }
        fromNumber = mCurIntNum;
        toNumber = toNum;
        numberType = 1;
        mPlayingState = STOPPED;
        start();
    }

    @Override
    public void setDuration(long duration) {
        this.duration = duration;
    }

    @Override
    public void setOnEndListener(INumberChangedListener callback) {
        mEndListener = callback;
    }

    public interface INumberChangedListener {
        void onEndFinish();
        void onNumOverGb();//大小为GB
    }
}
